package xq.gwt.ui.widgets.command;

import xq.gwt.mvc.controller.CommandController;
import xq.gwt.mvc.model.CommandModel;
import xq.gwt.mvc.view.CommandView;
import xq.gwt.mvc.view.ObservableViewImp;
import xq.gwt.ui.widgets.feedback.DefaultExceptionPresenter;
import xq.gwt.ui.widgets.feedback.DefaultFeedbackPresenter;

public class TreeNodeCommandPresenter extends ObservableViewImp implements CommandView{
	private ObservableTreeNode treeNode;
	protected CommandController controller;
	
	public TreeNodeCommandPresenter() {
		treeNode = new ObservableTreeNode();
		init();
	}
	
	public TreeNodeCommandPresenter(ObservableTreeNode treeNode) {
		this.treeNode = treeNode;
		init();
	}
	
	
	private void init(){
		treeNode.addTreeNodeClickedListener(new TreeNodeClickedListener() {
			
			@Override
			public void treeNodeClicked() {
				notifyViewChangedListeners();
				
			}
		});
		
		controller = new CommandController(this);
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		treeNode.setEnabled(enabled);
		
	}

	@Override
	public void setCaption(String caption) {
		treeNode.setTitle(caption);
		
	}

	@Override
	public void showFeedback(String feedback) {
		DefaultFeedbackPresenter presenter = new DefaultFeedbackPresenter();
		presenter.showFeedback(feedback);
		
	}

	@Override
	public void reportException(Exception ex) {
		DefaultExceptionPresenter exPresenter = new DefaultExceptionPresenter();
		exPresenter.showException(ex);
		
	}

	@Override
	public void executeStarted() {
		treeNode.setEnabled(false);
		
	}

	@Override
	public void excuteFinished() {
		treeNode.setEnabled(true);
		
	}

	public ObservableTreeNode getTreeNode() {
		return treeNode;
	}
	
	public void setModel(CommandModel model){
		controller.setCommandModel(model);
	}
}
